package middleware

import (
	"gitee.com/sdzxq/api_server/database"
	"gitee.com/sdzxq/api_server/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserExist() gin.HandlerFunc {
	return func(c *gin.Context){
		var jsonKey  models.User
		if err := c.ShouldBindJSON(&jsonKey); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"message": "用户信息格式有误",
				"success": false,
				"error": err.Error(),
			})
			c.Abort()
			return
		}
		if _, err := database.FindOneUser(jsonKey.UserID); err == nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"message": "用户ID已存在，请重新创建",
				"success": false,
				"error": err.Error(),
			})
			c.Abort()
			return
		}
		c.Next()
	}
}